home *** CD-ROM | disk | FTP | other *** search
- #include "global.h"
-
- extern char Amiga_drive[];
-
- /* Given a working directory and an arbitrary pathname, resolve them into
- * an absolute pathname. Memory is allocated for the result, which
- * the caller must free
- */
- char *
- pathname(cd,path)
- char *cd; /* Current working directory */
- char *path; /* Pathname argument */
- {
- register char *buf,*cp;
- char *cdtmp,*pathtmp;
-
- if(cd == NULLCHAR || path == NULLCHAR)
- return NULLCHAR;
- /* Strip any leading white space on args */
- while(*cd == ' ' || *cd == '\t')
- cd++;
- while(*path == ' ' || *path == '\t')
- path++;
-
- /* Allocate and initialize output buffer; user must free */
- buf = malloc((unsigned)strlen(Amiga_drive) +
- (unsigned)strlen(cd) + strlen(path) + 10); /* fudge factor */
- /*
- sprintf(buf, "%s", Amiga_drive);
- */
- /* Interpret path relative to cd only if it doesn't begin with "/" */
- if(path[0] != ':')
- crunch(buf, cd);
-
- crunch(buf,path);
-
- /* Special case: null final path means the root directory */
- if(buf[0] == '\0'){
- buf[0] = ':';
- buf[1] = '\0';
- }
-
- return buf;
- }
-
- /* Process a path name string, starting with and adding to
- * the existing buffer
- */
- static
- crunch(buf,path)
- char *buf;
- register char *path;
- {
- register char *cp;
-
-
- cp = buf + strlen(buf); /* Start write at end of current buffer */
-
- /* Now start crunching the pathname argument */
- /* Strip leading /'s; one will be written later */
-
- if (*path == ':' )
- {
- cp = buf;
- *cp++ = ':';
- path++;
- }
- while ( *path == '/' )
- {
- /* Hop up a level */
- if((cp = rindex(buf,'/')) == NULLCHAR)
- {
- cp = buf; /* Don't back up beyond root */
- *cp++ = ':';
- }
- *cp = '\0'; /* In case there's another / */
- path++; /* Skip "/" */
- }
-
- if(*path == '\0')
- {
- *cp++ = '\0'; /* no more, all done */
- return;
- }
- /* Look for parent directory references, either at the end
- * of the path or imbedded in it
- */
- for(;;)
- {
- if(*path == '\0')
- {
- break; /* no more, all done */
- }
- if(strcmp(path,"//") == 0 )
- {
- /* Hop up a level */
- if((cp = rindex(buf,'/')) == NULLCHAR)
- cp = buf; /* Don't back up beyond root */
- *cp = '\0'; /* In case there's another .. */
- path += 2; /* Skip ".." */
- while(*path == '/') /* Skip one or more slashes */
- {
- path++;
- }
- }
- else
- {
- if ( *path == '/' )
- path++;
- /* Ordinary name, copy up to next '/' or end of path */
- if ( *(cp-1) != ':' )
- {
- *cp++ = '/';
- }
- while(*path != '/' && *path != '\0')
- *cp++ = *path++;
- }
- }
- *cp++ = '\0';
- }
-